package com.ruoyi.controller.redismq;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.exception.AjaxException;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.SysUserAccount;
import com.ruoyi.system.domain.SysUserBillcash;
import com.ruoyi.system.mapper.SysUserAccountMapper;
import com.ruoyi.system.mapper.SysUserBillcashMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.other.MessageEntity;
import com.ruoyi.system.service.other.SendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 消息订阅者
 */
@Component
public class Receiver {

    private String pay = "pay";

    private String paid = "paid";

    @Autowired
    private SysUserAccountMapper sysUserAccountMapper;

    @Autowired
    private SysUserBillcashMapper sysUserBillcashMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SendService sendService;




    @Transactional
    public void receiveMessage(String message){
/*        if (message.equals("start")){
            messageConsumerService.run();
        }*/

        MessageEntity messageEntity = JSONUtil.toBean(message,MessageEntity.class);
        //支付回调
        if (messageEntity.getKey().equals(pay)){
            System.out.println("pay: " + messageEntity.getContent());
            SysOrder sysOrder = JSONUtil.toBean(messageEntity.getContent(),SysOrder.class);
            SysUserAccount sysUserAccount = sysUserAccountMapper.selectSysUserAccountById(sysOrder.getUserId());
            if (sysUserAccount==null){
                throw new AjaxException("account invalid");
            }
            BigDecimal oldBalance = sysUserAccount.getBalance();
            BigDecimal newBalance = sysUserAccount.getBalance().add(sysOrder.getOrderLastAmount());
            sysUserAccount.setBalance(newBalance);

            SysUserBillcash sysUserBillcash = new SysUserBillcash();
            sysUserBillcash.setRidCode(sysOrder.getOrderCode());
            sysUserBillcash.setChannelId(sysOrder.getChannelId());
            sysUserBillcash.setChannelName(sysOrder.getChannelName());
            sysUserBillcash.setAmount(oldBalance);
            sysUserBillcash.setChangeAmount(sysOrder.getOrderLastAmount());
            sysUserBillcash.setLastAmount(newBalance);
            SysUser sysUser = sysUserMapper.selectUserById(sysOrder.getUserId());
            if (sysUser==null){
                throw new AjaxException("user error");
            }
            sysUserBillcash.setUserId(sysUser.getUserId());
            sysUserBillcash.setUserCode(sysUser.getUserCode());
            sysUserBillcash.setLoginName(sysUser.getLoginName());
            sysUserBillcash.setBillcashType(0);
            sysUserBillcash.setBillcashRemark("支付完成");
            sysUserBillcash.setCreateTime(new Date());
            sysUserBillcash.setUpdateTime(new Date());
            try {
                sysUserBillcashMapper.insertSysUserBillcash(sysUserBillcash);
            }catch (Exception e){
                sendService.sendMessage(message);
                throw new BusinessException("try again");
            }

            //更新账户失败 放回队列
            if (sysUserAccountMapper.updateSysUserAccount(sysUserAccount)!=1){
                sendService.sendMessage(message);
                throw new BusinessException("try again222");
            }

        }else {
            System.out.println("paid: " + messageEntity.getContent());
        }
    }

}
